package org.utils;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.constant.RemoteServerType;
import org.remote.game.RemoteGameServer;
import org.remote.login.RemoteLoginServer;
import org.remote.payment.RemotePaymentServer;
import org.remote.social.RemoteSocialServer;

import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sojoys.artifact.manager.RemoteProxyManager;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月6日 上午10:28:30
 * @Description ：远程接口获取工具类
 */
public class RemoteUtil {

	static final String LOGIN_SYNC = "LOGIN_SYNC";
	static final String LOGIN_ASYN = "LOGIN_ASYN";
	static final String SOCIAL_SYNC = "SOCIAL_SYNC";
	static final String SOCIAL_ASYN = "SOCIAL_ASYN";
	static final String GAME_TOPIC = "GAME_TOPIC";
	static final String GAME_ASYN = "GAME_ASYN";
	static final String GAME_SYNC = "GAME_SYNC";
	static final String PAYMENT_ASYN = "PAYMENT_ASYN";
	static final String PAYMENT_SYNC = "PAYMENT_SYNC";

	
	
	static LoadingCache<String, Object> remoteCache = CacheBuilder.newBuilder().concurrencyLevel(8)
			.expireAfterWrite(10, TimeUnit.MINUTES).build(new CacheLoader<String, Object>() {
				@Override
				public Object load(String key) throws Exception {
					List<String> cmds = Splitter.on(StrUtil.COLON).omitEmptyStrings().splitToList(key);
					switch (cmds.get(0)) {
					case LOGIN_SYNC:
						return RemoteProxyManager.me().createReceive(RemoteLoginServer.class, RemoteServerType.LOGIN);
					case LOGIN_ASYN:
						return RemoteProxyManager.me().create(RemoteLoginServer.class, RemoteServerType.LOGIN,false);
					case SOCIAL_SYNC:
						return RemoteProxyManager.me().createReceive(RemoteSocialServer.class, RemoteServerType.SOCIAL);
					case SOCIAL_ASYN:
						return RemoteProxyManager.me().create(RemoteSocialServer.class, RemoteServerType.SOCIAL, false);
					case GAME_TOPIC:
						return RemoteProxyManager.me().create(RemoteGameServer.class, RemoteServerType.GAME, true);
					case GAME_ASYN:
						return RemoteProxyManager.me().create(RemoteGameServer.class, RemoteServerType.GAME + "_" + cmds.get(1), false);
					case GAME_SYNC:
						return RemoteProxyManager.me().createReceive(RemoteGameServer.class, RemoteServerType.GAME + "_" + cmds.get(1));
					case PAYMENT_SYNC:
						return RemoteProxyManager.me().createReceive(RemotePaymentServer.class, RemoteServerType.PAY);
					case PAYMENT_ASYN:
						return RemoteProxyManager.me().create(RemoteSocialServer.class, RemoteServerType.PAY, false);
					}
					return null;
				}
			});

	/**
	 * 游戏服-同步
	 * 
	 * @return
	 */
	public static RemoteGameServer gameSync(String serverId) {
		try {
			return (RemoteGameServer) remoteCache.get(GAME_SYNC+StrUtil.COLON+serverId);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 游戏服-异步
	 * 
	 * @return
	 */
	public static RemoteGameServer gameAsyn(String serverId) {
		try {
			return (RemoteGameServer) remoteCache.get(GAME_ASYN+StrUtil.COLON+serverId);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 游戏服-广播
	 * 
	 * @return
	 */
	public static RemoteGameServer gameTopic() {
		try {
			return (RemoteGameServer) remoteCache.get(GAME_TOPIC);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 登陆服-同步
	 * 
	 * @return
	 */
	public static RemoteLoginServer loginSync() {
		try {
			return (RemoteLoginServer) remoteCache.get(LOGIN_SYNC);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 登陆服-异步
	 * 
	 * @return
	 */
	public static RemoteLoginServer loginAsyn() {
		try {
			return (RemoteLoginServer) remoteCache.get(LOGIN_ASYN);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 社交服-同步
	 * 
	 * @return
	 */
	public static RemoteSocialServer socialSync() {
		try {
			return (RemoteSocialServer) remoteCache.get(SOCIAL_SYNC);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 社交服-异步
	 * 
	 * @return
	 */
	public static RemoteSocialServer socialAsyn() {
		try {
			return (RemoteSocialServer) remoteCache.get(SOCIAL_ASYN);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 登陆服-同步
	 *
	 * @return
	 */
	public static RemotePaymentServer paymentSync() {
		try {
			return (RemotePaymentServer) remoteCache.get(PAYMENT_SYNC);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 登陆服-异步
	 *
	 * @return
	 */
	public static RemotePaymentServer paymentAsyn() {
		try {
			return (RemotePaymentServer) remoteCache.get(PAYMENT_ASYN);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}
}
